#ifdef __cplusplus

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <new>

#ifndef RUI_STRING_H
#define RUI_STRING_H
#include <string>
#include <cstring>
#endif

namespace Rui_Ethereal {
    namespace file_io {
#ifndef BUFFER_SIZE
#define BUFFER_SIZE 1 << 16
#elif BUFFER_SIZE > 1 << 26
#warning "BUFFER_SIZE is set to 2^16, MAX_SIZE that doesn't cause stack overflow is 2^26."
#endif
        namespace exio {
            inline auto IsDigit(const char &_) -> bool {
                return '0' <= _ and _ <= '9'; }
            inline auto IsLetter(const char &_) -> bool {
                return ('A' <= _ and _ <= 'Z')
                or ('a' <= _ and _ <= 'z'); }
            inline auto IsValidChar(const char &_) -> bool {
                return IsDigit(_) or IsLetter(_); }
        }
        class BetterStreamReader {
            FILE *file_stream;
            char *buffer, *iter;
            long long int data_volume;
            
            inline auto GetChar(void) -> void {
                if(iter - buffer == data_volume) FileRead();
                else ++iter; }
        public:
            BetterStreamReader(FILE *file_stream = stdin) {
                buffer = new char [BUFFER_SIZE];
                this->file_stream = file_stream;
                // FileRead();
            }
            inline auto FileRead(void) -> void {
                data_volume = fread(buffer, 1, BUFFER_SIZE, file_stream);
                iter = buffer; }
            template <typename _Tp> BetterStreamReader& operator >> (_Tp &BSR) {
                bool negative = false;
                while(!exio::IsDigit(*iter)) {
                    negative ^= *iter == '-';
                    GetChar(); }
                BSR = 0;
                while(exio::IsDigit(*iter)) {
                    BSR = (BSR << 1) + (BSR << 3) + (*iter ^ '0');
                    GetChar(); }
                if(negative) BSR = ~BSR + 1;
                return *this;
            }
        };
        class BetterStreamWriter {
            FILE *file_stream;
            char *buffer, *iter;
            long long int data_volume;
            inline auto PutChar(const char &_) -> void {
                if(data_volume == BUFFER_SIZE) Flush();
                else buffer[data_volume++] = _; }
        public:
            BetterStreamWriter(FILE *file_stream = stdout) {
                buffer = new char [BUFFER_SIZE];
                iter = buffer;
                data_volume = 0;
                this->file_stream = file_stream;
            }
            ~BetterStreamWriter(void) { Flush(); }
            inline auto Flush(void) -> void {
                fwrite(buffer, 1, data_volume, file_stream);
                data_volume = 0; }
            template <typename _Tp> BetterStreamWriter& operator << (_Tp BSW) {
                if(BSW < 0) {
                    PutChar('-');
                    BSW = ~BSW + 1; }
                static char buf[44];
                short cur = 0;
                do buf[cur++] = static_cast <char> (BSW % 10 + '0'); while(BSW /= 10);
                while(cur) PutChar(buf[--cur]);
                return *this;
            }
            BetterStreamWriter& operator << (const char &BSW) {
                PutChar(BSW);
                return *this; }
        };
#undef BUFFER_SIZE
    }
}

#ifndef RUI_MACRO_DEF
#define RUI_MACRO_DEF
#define UnbindIOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)
#define MAG(_, BEGIN, END) for(auto _((BEGIN)); _ <= END; ++_)
#define ABA(_, BEGIN, END) for(auto _((BEGIN)); _ >= END; --_)
#define MAGs(_, BEGIN, END) for(auto _((BEGIN)); _ < END; ++_)
#define ABAs(_, BEGIN, END) for(auto _((BEGIN)); _ > END; --_)
#endif

Rui_Ethereal::file_io::BetterStreamReader in;
Rui_Ethereal::file_io::BetterStreamWriter out;

using lint = long long int;

const int MAXN = 4e2 + 5;
const int INF = 1e9 + 7;

int n, brightness[MAXN];
lint ans = -INF;
bool vis[MAXN];

#include <vector>
std::vector <int> G[MAXN];

template <typename _Tp> inline auto Max(const _Tp &a, const _Tp &b) -> _Tp {
    return a < b ? b : a; }

inline auto Dfs(const int &u, const int &fa) -> lint {
    vis[u] = true;
    lint ans = brightness[u];
    for(int v = 1; v <= n; ++v)
        if(!vis[v]) {
            brightness[v] += brightness[u];
            ans += Dfs(v, u);
            brightness[v] -= brightness[u];
        }
    vis[u] = false;
    return ans;
}

using namespace std;

main() {
    freopen("tsuki.in", "r", stdin), freopen("tsuki.out", "w", stdout);
    in.FileRead();

    in >> n;
    MAG(i, 1, n) in >> brightness[i];
    MAGs(i, 1, n) {
        int u, v;
        in >> u >> v;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }
    MAG(i, 1, n) {
        memset(vis, false, sizeof vis);
        ans = Max(ans, Dfs(i, 0));
    }
    out << ans;
    
    out.Flush();
    fclose(stdin), fclose(stdout);
}

// I LOVE WG !

#endif
